//
//  WelcomeViewController.swift
//  新浪微博01
//
//  Created by 李国伟 on 15/12/10.
//  Copyright © 2015年 李国伟. All rights reserved.
//

import UIKit
import SDWebImage
//头文件导入一次全局可用，loadview中不能使用计算性属性
class WelcomeViewController: UIViewController {

    //替换背景视图
    override func loadView() {
        view = backImageView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    //更新控件约束
    override func viewDidAppear(animated: Bool) {
        
        iconView.snp_updateConstraints { (make) -> Void in
            
        make.bottom.equalTo(self.view.snp_bottom).offset(-view.bounds.height + 200)
        }
        
        //实现动画
        wellcomLabel.alpha = 0
        UIView.animateWithDuration(1.6, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            
            self.view.layoutIfNeeded()
            
            
            }) { (_) -> Void in
            
            //慢慢显示label
            UIView.animateWithDuration(0.8, animations: { () -> Void in
                self.wellcomLabel.alpha = 1
                }, completion: { (_) -> Void in
               
                    //发送通知
                    NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewControllerNotification, object: nil, userInfo: nil)
            })
        }
    }
    
    //设置背景图片
    private lazy var backImageView = UIImageView(image:UIImage(named: "ad_background"))
    
    //设置头像
    private lazy var iconView: UIImageView = {
        
        let imageView = UIImageView()
        imageView.sd_setImageWithURL(UserAccountViewModel.shareModel.largeRUL, placeholderImage: UIImage(named: "avatar_default_big"))
        imageView.layer.cornerRadius = 45
        imageView.layer.masksToBounds = true
        return imageView
    }()
    
    //设置昵称label
    private lazy var wellcomLabel = UILabel(title: "欢迎回来",fontsize: 18, color: UIColor.blackColor())
}


//设置UI信息
extension WelcomeViewController{
    
    private func setupUI(){
        
         view.addSubview(iconView)
         view.addSubview(wellcomLabel)
        
        //设置控件的约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.height.equalTo(90)
            make.width.equalTo(90)
            make.centerX.equalTo(self.view)
            make.bottom.equalTo(self.view.snp_bottom).offset(-200)
        }
        
        wellcomLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(10)
            make.centerX.equalTo(iconView)
        }
    }
}